package com.smartystars.quizzer;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.*;

import android.content.Context;

public class MultiplayerNetworkThread extends Thread{
	private String Request;
	private String RequestString;
	private String SERVERIP = "domhey.no-ip.biz";
	private int SERVERPORT = 4090;
	private Context context;
	private MultiplayerStartupActivity MSA;
	protected int friendStatus = -5;
	
	
	public MultiplayerNetworkThread(String Request , String RequestString, Context context, MultiplayerStartupActivity MSA) {
		// TODO Auto-generated constructor stub
		this.Request = Request;
		this.RequestString = RequestString;
		this.context = context;
		this.MSA = MSA;
	}

	
	@Override
	public void run(){
		System.out.println("MultiplayerNetworkthread Started");
		Socket serverSocket;
		InetAddress serverAddr;
		
		
		if(Request.equals("REGISTER")){
		
		try {
            SocketAddress sockaddr = new InetSocketAddress(InetAddress.getByName(SERVERIP),SERVERPORT);
            serverSocket = new Socket();
            serverSocket.connect(sockaddr,5000);
			System.out.println("Created socket");
			schreibeNachricht(serverSocket, RequestString);
			((GlobalClass)context).RegisterStatus =Integer.valueOf( leseNachricht(serverSocket));
			
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			System.out.println("Unknown Host");
			((GlobalClass)context).RegisterStatus = -1;
			e.printStackTrace();
		
		} catch (SocketTimeoutException e){
			System.out.println("Timeout");
			((GlobalClass)context).RegisterStatus = -1;
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println("IOEX");
			
			
			((GlobalClass)context).RegisterStatus = -1;
			e.printStackTrace();
		}
		}else if(Request.equals("LOGIN")){
			try{

            SocketAddress sockaddr = new InetSocketAddress(InetAddress.getByName(SERVERIP),SERVERPORT);
            serverSocket = new Socket();
			serverSocket.connect(sockaddr,5000);

			System.out.println("Created socket");
			schreibeNachricht(serverSocket, RequestString);
			((GlobalClass)context).LoginStatus =Integer.valueOf( leseNachricht(serverSocket));
			
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			System.out.println("Unknown Host");
			((GlobalClass)context).LoginStatus = -1;
			e.printStackTrace();
		
		} catch (SocketTimeoutException e){
			System.out.println("Timeout");
			((GlobalClass)context).LoginStatus = -1;
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println("IOEX");
			
			
			((GlobalClass)context).LoginStatus = -1;
			e.printStackTrace();
		}
		}else if(Request.equals("CHECKFORFRIEND")){
			try{
                SocketAddress sockaddr = new InetSocketAddress(InetAddress.getByName(SERVERIP),SERVERPORT);
                serverSocket = new Socket();
                serverSocket.connect(sockaddr,5000);
				System.out.println("CheckForFriend: " + RequestString);
				schreibeNachricht(serverSocket,"CHECKFORFRIEND"+";"+ RequestString);
				float friendStatus2 =Float.valueOf( leseNachricht(serverSocket));
				friendStatus = (int)friendStatus2;
				
				
				MSA.addFriendToMap(RequestString, friendStatus);
				
			} catch (UnknownHostException e) {
				// TODO Auto-generated catch block
				System.out.println("Unknown Host");
				friendStatus = -2;
				e.printStackTrace();
				MSA.addFriendToMap(RequestString, friendStatus);

			
			} catch (SocketTimeoutException e){
				System.out.println("Timeout");
				friendStatus = -2;
				MSA.addFriendToMap(RequestString, friendStatus);

				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				System.out.println("IOEX");
				
				
				friendStatus = -2;
				e.printStackTrace();
				MSA.addFriendToMap(RequestString, friendStatus);

			}
		}
		

	}
	
	void schreibeNachricht(java.net.Socket socket, String nachricht)
			throws IOException {


		PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(
				socket.getOutputStream()));
		printWriter.print(nachricht);
		printWriter.flush();

        printWriter.close();
	}
	
	String leseNachricht(java.net.Socket socket) throws IOException {
		try {

			BufferedReader bufferedReader = new BufferedReader(
					new InputStreamReader(socket.getInputStream()));
			char[] buffer = new char[200];

			int anzahlZeichen = bufferedReader.read(buffer, 0, 200); // blockiert
																		// bis
																		// Nachricht
																		// empfangen
			if ((anzahlZeichen <= 200) && (anzahlZeichen >= 0)) {
				String nachricht = new String(buffer, 0, anzahlZeichen);
                bufferedReader.close();
				return nachricht;

			} else {
				
				return null;
			}
		} catch (IOException e) {

			return null;
		}
	}
	
	
}
